import type { AxiosResponse } from "axios"
import type { ResponseTableData } from "../common.ts"
import type * as Agent from "./type.ts"
import { request } from "@/http/axios"
import { isEmpty } from "lodash-es"

const baseURL = import.meta.env.VITE_TRAIN_API

/** 增 */
export function createAgentDataApi(data: FormData) {
  return request({
    baseURL,
    headers: {
      "Content-Type": "multipart/form-data"
    },
    url: "/toksim/resource/agent/add",
    method: "post",
    data
  })
}

/** 删 */
export function deleteAgentDataApi(agent_ids: string[]) {
  return request({
    baseURL,
    url: `/toksim/resource/agent/delete`,
    method: "post",
    data: {
      agent_ids
    }
  })
}

/** 改 */
export function updateAgentDataApi(data: FormData) {
  return request({
    baseURL,
    headers: {
      "Content-Type": "multipart/form-data"
    },
    url: "/toksim/resource/agent/edit",
    method: "post",
    data
  })
}

/** 查 */
export function getAgentDataApi(params: Agent.PageTableRequestData) {
  if (isEmpty(params.project_id)) {
    delete params.project_id
  }
  return request<Agent.SearchAgentTableResponseData>({
    baseURL,
    url: "/toksim/resource/agent/search",
    method: "post",
    data: params
  })
}

export function getAgentListApi() {
  return request<Agent.SearchAgentTableResponseData>({
    baseURL,
    url: "/toksim/resource/agent/list",
    method: "get"
  })
}

/** 根据id查询资源库信息 */
export function getAgentInfoApi(id: string) {
  return request<Agent.AgentViewInfoResponseData>({
    baseURL,
    url: `/toksim/resource/agent/get/${id}`,
    method: "get"
  })
}
/** 获取智能体类型 */
export function getAgentTypeOptionsApi() {
  return request<ApiResponseData<{ type: string[] }>>({
    baseURL,
    url: `/toksim/resource/agent/type/get`,
    method: "get"
  })
}

/** 下载智能体资源 */
export function downloadAgentApi(id: string) {
  return request<AxiosResponse>({
    baseURL,
    headers: {
      "Content-Type": "application/octet-stream"
    },
    responseType: "blob",
    url: `/toksim/resource/agent/download`,
    method: "POST",
    data: {
      agent_id: id
    }
  })
}

// 智能体详情
/** 查询代码树状 */
export function structApi(params: { agent_id: string, parent_id: string }) {
  return request<AxiosResponse>({
    baseURL,
    url: `/toksim/resource/agent_code/get`,
    method: "get",
    params
  })
}

/** 查询代码文件内容 */
export function filecontentApi(id: string) {
  return request<AxiosResponse>({
    baseURL,
    url: `/toksim/resource/agent_code/content/get/${id}`,
    method: "get"
  })
}
/** 更新引擎文件内容 */
export function saveAgentFile(data: FormData) {
  return request<ApiResponseData<undefined>>({
    baseURL,
    headers: {
      "Content-Type": "multipart/form-data"
    },
    url: "/toksim/resource/agent_code/update_code",
    method: "post",
    data
  })
}
/** 文件目录重命名 */
export function agentFileRenameApi(data: any) {
  return request({
    baseURL,
    url: `/toksim/resource/agent_code/rename`,
    method: "post",
    data
  })
}
/** 增 引擎文件 */
export function addAgentFileApi(data: any) {
  return request({
    baseURL,
    url: `/toksim/resource/agent_code/add`,
    method: "post",
    data
  })
}
/** 删 引擎文件 */
export function deleteAgentFileApi(ids: string[]) {
  return request({
    baseURL,
    url: `/toksim/resource/agent_code/delete`,
    method: "post",
    data: {
      ids
    }
  })
}
/** 移动 引擎文件 */
export function moveAgentFileApi(data: any) {
  return request({
    baseURL,
    url: `/toksim/resource/agent_code/move`,
    method: "post",
    data
  })
}
/** 复制 引擎文件 */
export function copyAgentFileApi(data: any) {
  return request({
    baseURL,
    url: `/toksim/resource/agent_code/copy`,
    method: "post",
    data
  })
}
/** 更换主文件 */
export function updateFileMainApi(id: string) {
  return request({
    baseURL,
    url: `/toksim/resource/agent_code/update_main/${id}`,
    method: "post"
  })
}
/** 详情列表 */
export function trainedListApi(id: string, data: any) {
  return request<AxiosResponse<ResponseTableData<Agent.TrainAgentInfo>>>({
    baseURL,
    url: `/toksim/resource/agent/${id}/trained/search`,
    method: "post",
    data
  })
}

/** 详情列表删除 */
export function trainedDeleteApi(obj: { agent_id: string, ids: string[] }) {
  return request<AxiosResponse>({
    baseURL,
    url: `/toksim/resource/agent/${obj.agent_id}/trained/delete`,
    method: "post",
    data: {
      trained_agent_ids: obj.ids // 传递数组参数
    }
  })
}

/** 详情列表下载 */
export function trainedDownloadApi(id: string, data: any) {
  return request<AxiosResponse>({
    baseURL,
    headers: {
      "Content-Type": "application/octet-stream"
    },
    responseType: "blob",
    url: `/toksim/resource/agent/${id}/trained/download`,
    method: "post",
    data
  })
}

/** 详情列表发布 */
export function trainedPublishApi(obj: { agent_id: string, ids: string[] }) {
  return request<AxiosResponse>({
    baseURL,
    url: `/toksim/resource/agent/${obj.agent_id}/trained/publish`,
    method: "post",
    data: {
      trained_agent_ids: obj.ids // 传递数组参数
    }
  })
}

/** 导入 */
export function importApi(data: any) {
  return request<AxiosResponse>({
    baseURL,
    url: `/toksim/resource/agent/import`,
    method: "post",
    data
  })
}

/** 另存为 */
export function saveCopyApi(params: Agent.PageTableRequestData) {
  return request<AxiosResponse>({
    baseURL,
    url: `/toksim/resource/agent/save_copy`,
    method: "post",
    data: params
  })
}

// 新增/更新/删除附件
export function documentSaveApi(data: any) {
  return request<AxiosResponse>({
    baseURL,
    headers: {
      "Content-Type": "multipart/form-data"
    },
    url: `/toksim/resource/document/save_files`,
    method: "post",
    data
  })
}

/** 根据业务id获取文档信息 */
export function documentAssociateApi(associate_id: string) {
  return request<AxiosResponse>({
    baseURL,
    url: `/toksim/resource/document/associate/get/${associate_id}`,
    method: "get",
  })
}

// 查看文档
export function documentGetApi(file_id: string) {
  return request<AxiosResponse>({
    baseURL,
    url: `/toksim/resource/document/get/${file_id}`,
    method: "get",
    responseType: "blob",
    headers: {
      "Accept": "application/pdf,application/octet-stream"
    }
  })
}
// 下载文档
export function documentDownloadApi(data: any) {
  return request<AxiosResponse>({
    baseURL,
    responseType: "blob",
    url: `/toksim/resource/document/download`,
    method: "post",
    data
  })
}
